package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.domain.BaseMeterialMove;
import com.ruoyi.base.mapper.BaseFruitMoveMapper;
import com.ruoyi.base.mapper.BaseMeterialMoveMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseMeterialStockMapper;
import com.ruoyi.base.domain.BaseMeterialStock;
import com.ruoyi.base.service.IBaseMeterialStockService;

/**
 * 物料库存操作Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-04
 */
@Service
public class BaseMeterialStockServiceImpl implements IBaseMeterialStockService {
    @Autowired
    private BaseMeterialStockMapper baseMeterialStockMapper;

    @Autowired
    private BaseMeterialMoveMapper baseMeterialMoveMapper;
    /**
     * 查询物料库存操作
     *
     * @param id 物料库存操作主键
     * @return 物料库存操作
     */
    @Override
    public BaseMeterialStock selectBaseMeterialStockById(Long id) {
        return baseMeterialStockMapper.selectBaseMeterialStockById(id);
    }

    /**
     * 查询物料库存操作列表
     *
     * @param baseMeterialStock 物料库存操作
     * @return 物料库存操作
     */
    @Override
    public List<BaseMeterialStock> selectBaseMeterialStockList(BaseMeterialStock baseMeterialStock) {
        return baseMeterialStockMapper.selectBaseMeterialStockList(baseMeterialStock);
    }

    /**
     * 新增物料库存操作
     *
     * @param baseMeterialStock 物料库存操作
     * @return 结果
     */
    @Override
    public int insertBaseMeterialStock(BaseMeterialStock baseMeterialStock) {
        return baseMeterialStockMapper.insertBaseMeterialStock(baseMeterialStock);
    }

    /**
     * 修改物料库存操作
     *
     * @param baseMeterialStock 物料库存操作
     * @return 结果
     */
    @Override
    public int updateBaseMeterialStock(BaseMeterialStock baseMeterialStock) {
        baseMeterialStock.setUpdateTime(DateUtils.getNowDate());
        BaseMeterialMove baseMeterialMove = new BaseMeterialMove();
        baseMeterialMove.setMeterialId(baseMeterialStock.getId());
        baseMeterialMove.setCreateTime(DateUtils.getNowDate());
        baseMeterialMove.setMeterialMoveNumber(baseMeterialStock.getMeterialMoveNum().longValue());
        baseMeterialMove.setMeterialMoveStatus(baseMeterialStock.getMeterialMoveStatus().toString());
        if (baseMeterialStock.getMeterialMoveStatus() == 0) {
            baseMeterialMove.setReceiveBy(baseMeterialStock.getReceiveBy());
            baseMeterialStock.setNumber(baseMeterialStock.getNumber()-baseMeterialStock.getMeterialMoveNum());
        } else {
            baseMeterialStock.setNumber(baseMeterialStock.getNumber()+baseMeterialStock.getMeterialMoveNum());
        }
        baseMeterialMoveMapper.insertBaseMeterialMove(baseMeterialMove);
        return baseMeterialStockMapper.updateBaseMeterialStock(baseMeterialStock);
    }

    /**
     * 批量删除物料库存操作
     *
     * @param ids 需要删除的物料库存操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseMeterialStockByIds(Long[] ids) {
        return baseMeterialStockMapper.deleteBaseMeterialStockByIds(ids);
    }

    /**
     * 删除物料库存操作信息
     *
     * @param id 物料库存操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseMeterialStockById(Long id) {
        return baseMeterialStockMapper.deleteBaseMeterialStockById(id);
    }
}
